﻿@model IList<ScheduleTaskModel>
@using Telerik.Web.Mvc.UI;
@using System.Linq;
@{
    ViewBag.Title = T("Admin.System.ScheduleTasks").Text;
}

<div class="section-header">
    <div class="title">
		<i class="far fa-clock"></i>
        @T("Admin.System.ScheduleTasks")
    </div>
</div>

<div class="table-responsive">
	<table class="table admin-table" id="schedule-tasks-grid">
		<thead>
			<tr>
				<th>@T("Admin.System.ScheduleTasks.Name")</th>
				<th class="text-center">@T("Admin.System.ScheduleTasks.Enabled")</th>
				<th>@T("Admin.System.ScheduleTasks.CronExpression")</th>
				<th style="min-width: 150px">@T("Admin.System.ScheduleTasks.LastStart")</th>
				<th>@T("Admin.System.ScheduleTasks.NextRun")</th>
				<th>&nbsp;</th>
			</tr>
		</thead>
		<tbody>
			@foreach (var task in Model)
			{
                var isRunning = task.LastHistoryEntry.IsRunning;
				<tr data-task-id='@task.Id' class='@(isRunning ? "active-row" : "")@(task.Enabled ? "" : " table-light")'>
					<td>
						<a href='@task.EditUrl'>@task.Name</a>
					</td>
					<td class="text-center">
                        @Html.SymbolForBool(task.Enabled)
                    </td>
					<td>
						<div>@task.CronExpression</div>
						@if (task.CronDescription.HasValue())
						{
							<div class="muted">@task.CronDescription</div>
						}
					</td>
					<!-- LastStart -->
					<td style="min-width: 300px">
						<div class="last-run-info">
							@{ Html.RenderPartial("_LastRun", task); }
						</div>
					</td>
					<!-- NextRun -->
					<td style="min-width: 150px">
						<div class="next-run-info">
							@{ Html.RenderPartial("_NextRun", task); }
						</div>
						<div class='task-progress text-muted@(isRunning ? "" : " hide")' data-task-id='@task.Id' data-running='@(isRunning ? "true" : "false")'></div>
					</td>
					<!-- Actions -->
					<td class="omega text-truncate">
						<a href='@task.EditUrl' class="btn btn-secondary btn-sm mb-1">@T("Common.Edit")</a>
						<a href='@task.ExecuteUrl' data-task-id='@task.Id' class='btn btn-success btn-sm mb-1 btn-run-task@(isRunning ? " hide" : "")'>
							@T("Admin.System.ScheduleTasks.RunNow")
						</a>
						<a href='@task.CancelUrl' data-task-id='@task.Id' class='btn btn-danger btn-sm mb-1 btn-cancel-task@(!isRunning ? " hide" : "")' onclick='return confirm(@T("Admin.Common.AreYouSure").JsText);'>
							@T("Common.Cancel")
						</a>
					</td>
				</tr>
			}
		</tbody>
	</table>
</div>

<script>
	$(function () {
		SmartStore.Admin.TaskWatcher.startWatching({
			context: $('#schedule-tasks-grid'),
			elementsSelector: '.task-progress',
			defaultProgressMessage: @T("Admin.System.ScheduleTasks.RunNow.IsRunning").JsText,
			pollUrl: '@Url.Action("GetRunningTasks")',
			onTaskStarted: function (task, el) {
				var tr = el.closest('tr');
				tr.addClass('active-row');
				tr.find('.btn-run-task').addClass('hide').next().removeClass('hide');
				tr.find('.next-run-info').addClass('hide');
			},
			onTaskCompleted: function (taskId, el) {
				var tr = el.closest('tr');
				tr.removeClass('active-row').find('.btn-run-task').removeClass('hide').next().addClass('hide');
				$.ajax({
					cache: false,
					global: false,
					type: 'POST',
					url: '@Url.Action("GetTaskRunInfo")',
					data: { id: taskId },
					success: function (data) {
						tr.find('.next-run-info').html(data.nextRunHtml).removeClass("hide");
						tr.find('.last-run-info').html(data.lastRunHtml);
					}
				});
			}
		});
	});
</script>

